package com.cduestc.mylogin;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class PwdChange extends AppCompatActivity {

    EditText newPwd,newPwd2;
    Button changePwd;
    SharedPreferences shareP;
    CheckBox changePwdCheckbox;
    public static String keepPwd = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_change);

        newPwd = this.findViewById(R.id.newPwd);
        newPwd2 = this.findViewById(R.id.newPwd2);
        changePwd = this.findViewById(R.id.changePwd);
        changePwdCheckbox = this.findViewById(R.id.changePwdCheckbox);
        shareP = this.getSharedPreferences("myMessage",MODE_PRIVATE);


        changePwdCheckbox.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(changePwdCheckbox.isChecked()){
                    newPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    newPwd2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    newPwd2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }

        });

        changePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newP1 = newPwd.getText().toString().trim();
                String newP2 = newPwd2.getText().toString().trim();
                if(newP1.length() < 6){
                    Toast.makeText(PwdChange.this, "密码长度至少为6位", Toast.LENGTH_SHORT).show();
                    return;
                }else if(!newP1.equals(newP2)){
                    Toast.makeText(PwdChange.this, "两次输入密码必须一致", Toast.LENGTH_SHORT).show();
                    newPwd.setText("");
                    newPwd2.setText("");
                    return;
                }else{
                    Toast.makeText(PwdChange.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    long check = System.currentTimeMillis();
                    long checkCode =  shareP.getLong("loseTime",0);
                    if(checkCode == 0 || checkCode < check){
                        keepPwd = newP1;
                        Intent intent3 = new Intent(PwdChange.this,MyLoginActivity.class);
                        startActivity(intent3);
                        return;
                    }

                    SharedPreferences.Editor seChangePwd = shareP.edit();
                    seChangePwd.putString("password",newP1);
                    seChangePwd.commit();
                    Intent inte = new Intent(PwdChange.this,MyLoginActivity.class);
                    startActivity(inte);
                }
            }
        });
    }
}